<?php
class BlogAction extends Action{
	private $blog;
	private $bc;

	public function __construct()
	{
		parent::__construct();
		$this->blog = new BlogModel();
		$this->bc = new BlogCommentModel();

		if($this->session->getAttr("uid") == null){
			$this->forward("Index");
		}
	}
	
	protected function init()
	{

	}
	public function display()
	{
		
	}
	public function doAddBC()
	{
		$signatures = array(
			'bc_blog_id' => array('name' => '作品ID', 'required' => true, 'type' => 'int'),
			'bc_author_id' => array('name' => '作者ID', 'required' => true, 'type' => 'int'),
			'bc_text' => array('name' => '评论内容', 'required' => true, 'type' => 'string')
			);

		$this->checkVars($this->post, $signatures);

		$this->bc->create();

		if($this->bc->add()){
			$msg = "添加评论成功";
			/*
			$work->addCondition('work_id', '=', $this->wc->wc_work_id, "");
			$work->findOne();
			$work->reply_number++;
			$work->addCondition('work_id', '=', $this->wc->wc_work_id, "");
			$work->update();
			 */
		}else{
			$msg = "添加评论失败!";
		}
		$this->showMessage($msg);
	}
	public function doView()
	{
		$this->addBlog();
	}
	public function addBlog()
	{
		global $privacy;
		$this->smarty->assign('privacy', $privacy);
		$this->smarty->display("Blog-addBlog.tpl");
	}
	public function doAddBlog()
	{
		$sigs = array(
			'blog_title' => array( 'required' => true, 'name' => '日志标题', 'type' => 'string'),
			'blog_text' => array( 'required' => true, 'name' => '日志内容', 'type' => 'string'),
			'blog_privacy' => array('required' => true, 'name' => '隐私策略', 'type' => 'int')
			);

		$this->checkVars($this->post, $sigs);

		$this->blog->create();
		$this->blog->author_id = $this->session->getAttr('uid');

		if($this->blog->add())
		{
			$msg = "发表日志成功！";
		}
		else
		{
			$msg = "发表日志失败！";
		}
		$this->showMessage($msg);
	}
	public function showBlogs()
	{
		$uid = $this->get['uid'] ? $this->get['uid'] : $this->session->getAttr('uid');

		$uid = (int) $uid;
		$this->blog->addCondition('author_id', '=', $uid, '');
		$blogs = $this->blog->find();

		$this->smarty->assign('blogs', $blogs);
		$this->smarty->display("Blog-showBlogs.tpl");
	}
	public function showBlog()
	{
		$bid = $this->get['bid'];
		if(!isset($bid))
		{
			$this->showMessage('您要查看的日志不存在！');
		}
		$bid = (int)$bid;
		$this->blog->addCondition('blog_id', '=', $bid, '');
		$this->bc->addCondition('bc_blog_id', '=', $bid, '');
		$blog = $this->blog->findOne();
		$comments = $this->bc->find();

		$this->smarty->assign('blog', $blog);
		$this->smarty->assign('comments', $comments);
		$this->smarty->display("Blog-showBlog.tpl");
	}
};
?>

